package com.rzjm.api.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rzjm.api.config.RequestDataHelper;
import com.rzjm.api.dao.CitizenDao;
import com.rzjm.api.dao.SurveyUserRecordDao;
import com.rzjm.api.dao.SurveyUserRecordInfoDao;
import com.rzjm.api.entity.*;
import com.rzjm.api.exception.TransactionalException;
import com.rzjm.api.service.ParameterService;
import com.rzjm.api.service.SurveyUserRecordInfoService;
import com.rzjm.api.service.SurveyUserRecordService;
import com.rzjm.api.util.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 问卷调查的用户填报数据Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-05
 */
@Service
public class SurveyUserRecordServiceImpl extends BaseServiceImpl<SurveyUserRecordDao, SurveyUserRecord> implements SurveyUserRecordService {

    @Resource
    private SurveyUserRecordDao recordMapper;
    @Resource
    private SurveyUserRecordInfoDao recordInfoMapper;
    @Resource
    private SurveyUserRecordInfoService recordInfoService;
    @Resource
    private CitizenDao citizenDao;
    @Resource
    private ParameterService parameterService;



    @Override
    @Transactional
    public int addRecord(int surveyId, Integer roomId, User user, SysUser sysUser,
                         Double lng, Double lat, String answer,Integer check_type) {
        try{
            SurveyUserRecord record = new SurveyUserRecord();
            record.setSurveyId(surveyId);
            record.setWxUserId(user.getId());
            record.setSysUserId(sysUser.getUserId());
            record.setNickName(user.getNickName());
            record.setPhoneNum(user.getPhonenum());
            record.setTownId(sysUser.getTownId());
            record.setVillageId(sysUser.getVillageId());
            record.setRoomId(roomId);
            record.setLng(lng);
            record.setLat(lat);
            record.setCreateTime(new Date());
            record.setCheckType(check_type);

            recordInfoService.addNowYearRecordTable();

            Parameter parameter = parameterService.getParameterById(1);
            if(parameter != null){
                record.setRecordTableName(parameter.getSurveyRecordInfoName());
            }else{
                String tableNameNow = "survey_user_record_info_" + DateUtil.getMonthSimpleStr(new Date());
                record.setRecordTableName(tableNameNow);
            }

            Citizen citizen = citizenDao.selectById(roomId);
            if(citizen!=null){
                record.setRoomNo(citizen.getRoomNo());
                record.setHolderPhone(citizen.getHolderPhone());
                record.setHolderName(citizen.getHolderName());
            }
            int ret = recordMapper.insert(record);
            if(ret <= 0){
                throw new TransactionalException("recordMapper insert error");
            }

            Long recordId = record.getId();


            Map<String,Object> map = new HashMap<>();
            map.put("survey_user_record_info", record.getRecordTableName());
            RequestDataHelper.setRequestData(map);

            if(StrUtil.isNotEmpty(answer)){
                JSONArray jsonArray = JSONUtil.parseArray(answer);
                for (int i = 0;i < jsonArray.size();i++){
                    JSONObject jsonObject = (JSONObject)jsonArray.get(i);

                    SurveyUserRecordInfo recordInfo = new SurveyUserRecordInfo();
                    recordInfo.setRecordId(recordId);
                    recordInfo.setUserId(user.getId());
                    recordInfo.setQuestionId(jsonObject.getInt("question_id"));
                    recordInfo.setQuestionType(jsonObject.getInt("question_type"));
                    if(jsonObject.getInt("question_type") == 0){//单选
                        recordInfo.setAnswer(jsonObject.getInt("check_value").toString());
                    }else if(jsonObject.getInt("question_type") == 1){//输入
                        recordInfo.setAnswer(jsonObject.getStr("check_value"));
                    }else if(jsonObject.getInt("question_type") == 2){//多选
                        recordInfo.setAnswer(jsonObject.getStr("checkBoxs"));
                    }else if(jsonObject.getInt("question_type") == 3){//图片
                        recordInfo.setAnswer(jsonObject.getStr("fileList"));
                    }
                    recordInfo.setCreateTime(new Date());
                    ret = recordInfoMapper.insert(recordInfo);
                    if(ret <= 0){
                        throw new TransactionalException("recordMapper insert error");
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("addRecord insert error");
        }
        return 1;
    }
}
